package com.eusoft.demo.controller;

import com.eusoft.demo.entity.*;
import com.eusoft.demo.query.CarListQuery;
import com.eusoft.demo.query.QueryCheckCarPlateCode;
import com.eusoft.demo.service.*;
import com.eusoft.demo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

/**
 * @authoe DevMing
 * @data 2021/8/19 10:16
 */
@Controller
@RequestMapping("/carManage")
public class CarManageController {

    @Autowired
    private ICarService carService;

    @Autowired
    private ICarTypeService carTypeService;

    @Autowired
    private ICarStateService carStateService;

    @Autowired
    private ICarUserService carUserService;

    @GetMapping("/toList")
    public String toList() {
        return "carManage/carManageList";
    }


    @GetMapping("/carTypeList")
    @ResponseBody
    public Result<CarType> carTypeList() {
        return ResultUtil.success(carTypeService.getCarTypeList());
    }

    @GetMapping("/carStateList")
    @ResponseBody
    public Result<CarState> carStateList() {
        return ResultUtil.success(carStateService.getCarStateList());
    }

    /**
     * 获取车辆列表
     *
     * @param query 参数对象
     * @return code, message, data
     */
    @GetMapping("/carList")
    @ResponseBody
    public Result<Map<String, Object>> carList(CarListQuery query) {
        return ResultUtil.success(carService.getCarList(query));
    }

    /**
     * 删除车辆
     *
     * @param aid 车辆aid
     * @return code, message, data
     */
    @PostMapping("/delCar")
    @ResponseBody
    public Result<Object> delCar(@RequestParam Integer aid) {
        boolean success = carService.delCarByCarId(aid);
        if (!success) {
            return ResultUtil.error(-1, "失败");
        }
        return ResultUtil.success(null);
    }

    /**
     * 获取车辆详情
     *
     * @param aid 车辆aid
     * @return
     */
    @GetMapping("/carDetail")
    @ResponseBody
    public Result<Object> carDetail(@RequestParam Integer aid) {
        return ResultUtil.success(carService.getCarById(aid));
    }

    /**
     * 获取车辆使用用户列表
     *
     * @param aid      车辆aid
     * @param page     页码
     * @param pageRows 每页数据量
     * @return
     */
    @GetMapping("carUserList")
    @ResponseBody
    public Result<User> carUserList(@RequestParam Integer aid,
                                    @RequestParam(required = false) Integer page,
                                    @RequestParam(required = false) Integer pageRows) {
        if (aid == null) {
            return ResultUtil.error(-1, "查询失败，请检查参数");
        }
        return ResultUtil.success(carUserService.getUserListByCarAid(aid, page, pageRows));
    }

    /**
     * 获取修改车辆信息
     *
     * @param aid 车辆aid
     * @return
     */
    @GetMapping("/carInfo")
    @ResponseBody
    public Object carInfo(@RequestParam Integer aid) {
        return ResultUtil.success(carService.getUpdateCar(aid));
    }

    /**
     * 校验相同车牌号的其他车辆是否存在
     *
     * @return isExists ”true 存在，false 不存在“
     */
    @GetMapping("/checkCarPlateCode")
    @ResponseBody
    public Object checkCarPlateCode(QueryCheckCarPlateCode query) {
        return ResultUtil.success(carService.checkCarPlateCode(query));
    }


    /**
     * 保存车辆
     *
     * @param car 车辆信息
     * @return
     */
    @PostMapping("/saveCar")
    @ResponseBody
    public Object saveCar(Car car) {
        return null;
    }
}
